package com.chen.app.controller;

import com.chen.app.util.SysCaptchaService;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

@RestController
public class SysController {

    @Autowired
    private SysCaptchaService sysCaptchaService;

    @GetMapping("captcha.jpg")
    public void captcha(HttpServletResponse response) throws IOException{
        response.setHeader("Cache-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");

        BufferedImage captcha = sysCaptchaService.getCaptcha();
        ServletOutputStream outputStream = response.getOutputStream();

        ImageIO.write(captcha,"jpg",outputStream);
        IOUtils.closeQuietly(outputStream);
    }
}
